package com.cpi.cornpp.manualcharges.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.manualcharges.dto.ManualChargesDTO;
import com.cpi.cornpp.manualcharges.dto.ManualChargesDetailsDTO;
import com.cpi.cornpp.manualcharges.exception.ManualChargesException;
import com.cpi.cornpp.manualcharges.view.ManualChargesDetailsView;
import com.cpi.cornpp.manualcharges.view.ManualChargesView;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.StringUtils;
import com.cs.common.utils.TimeUtils;

/**
 * Cargos Manuales Converter
 * 
 * @author Edgar Joao
 *
 */
public class ManualChargesConverter {
	/**
	 * Helper que obtiene la descripcion del tipo de Cargo
	 * @param chargeType
	 * @return
	 */
	private String getChargeTypeDescription(String chargeType){
		String value = StringUtils.EMPTY_STRING;
		if(org.apache.commons.lang.StringUtils.equals(chargeType, "P")){
			value = "Ponderado";
		}else if(org.apache.commons.lang.StringUtils.equals(chargeType, "M")){
			value = "Manual";
		}else if(org.apache.commons.lang.StringUtils.equals(chargeType, "U")){
			value = "Unitario";
		}
		return value;
	}
	/**
	 * Metodo que obtiene el Codigo del Tipo de cargo Manual
	 * @author Edgar Joao
	 * @param chargeType
	 * @return
	 */
	public String getChargeTypeCode(String chargeType){
		String value = StringUtils.EMPTY_STRING;
		if(org.apache.commons.lang.StringUtils.equals(chargeType, "Ponderado")){
			value = "P";
		}else if(org.apache.commons.lang.StringUtils.equals(chargeType, "Manual")){
			value = "M";
		}else if(org.apache.commons.lang.StringUtils.equals(chargeType, "Unitario")){
			value = "U";
		}
		return value;
	}
	/**
	 * Converter DTO to View
	 * 
	 * @param dto
	 * @return
	 * @throws ManualChargesException
	 */
	public ManualChargesView converterManualChargesDtoToView(ManualChargesDTO dto) throws ManualChargesException{
		ManualChargesView view = null;
		try{
			if(dto != null){
				view = new ManualChargesView();
				view.setManualChargeId(dto.getManualChargeId());
				view.setVersionId(dto.getVersionId());
				view.setCostElementId(dto.getCostElementId());
				view.setShortName(dto.getShortName());
				view.setDescription(dto.getDescription());
				view.setAmount(NumberUtil.convertPrice(dto.getAmount()));				
				view.setChargeType(getChargeTypeDescription(dto.getChargeType()));
				view.setUserId(dto.getUserId());
				view.setDate(dto.getDate() != null ? TimeUtils.convertJavaDateToString(dto.getDate()) : StringUtils.EMPTY_STRING);
			}			
		}catch( Exception exception ){
			ManualChargesException manualChargesException=null;
			exception.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw manualChargesException;
		}
		return view;
	}
	/**
	 * View to DTO
	 * @param view
	 * @return
	 * @throws ManualChargesException
	 */
	public ManualChargesDTO converterManualChargesViewToDto(ManualChargesView view) throws ManualChargesException {
		ManualChargesDTO dto = null;
		try{
			if(view != null){
				dto = new ManualChargesDTO();
				dto.setManualChargeId(view.getManualChargeId());
				dto.setVersionId(view.getVersionId());
				dto.setShortName(view.getShortName());
				dto.setCostElementId(view.getCostElementId());
				dto.setDescription(view.getDescription());
				dto.setAmount(NumberUtil.parseDouble(view.getAmount()));
				dto.setChargeType(view.getChargeType());
				dto.setUserId(view.getUserId());
			}
		}catch( Exception exception ){
			ManualChargesException manualChargesException=null;
			exception.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw manualChargesException;
		}
		return dto;
	}
	/**
	 * Convert DTOs to Views
	 * @param manualChargesDTOs
	 * @return
	 * @throws ManualChargesException
	 */
	public List convertDTOsToViews(List manualChargesDTOs) throws ManualChargesException {
		Iterator iterator 		= null;
		List manualChargesList  = null;
		ManualChargesDTO dto 	= null;
		ManualChargesView view  = null;
		try{
			manualChargesList = new ArrayList(0);
			iterator = manualChargesDTOs.iterator();
			while( iterator.hasNext() ){
				dto  = (ManualChargesDTO) iterator.next();
				view = this.converterManualChargesDtoToView(dto);
				manualChargesList.add(view);
			}			
		}catch(ManualChargesException manualChargesException){
			throw manualChargesException;
		}catch( Exception exception ){
			ManualChargesException manualChargesException=null;
			exception.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw manualChargesException;
		}
		return manualChargesList;
	}
	/**
	 * convert Views to DTOs
	 * 
	 * @param manualCHargesViews
	 * @return
	 * @throws ManualChargesException
	 */
	public List convertViewsToDTOs(List manualCHargesViews) throws ManualChargesException {
		Iterator iterator 		= null;
		List manualChargesList  = null;
		ManualChargesDTO dto 	= null;
		ManualChargesView view  = null;
		try{
			manualChargesList = new ArrayList(0);
			iterator = manualCHargesViews.iterator();
			while( iterator.hasNext() ){
				view  = (ManualChargesView) iterator.next();
				dto   = this.converterManualChargesViewToDto(view); 
				manualChargesList.add(dto);
			}			
		}catch(ManualChargesException manualChargesException){
			throw manualChargesException;
		}catch( Exception exception ){
			ManualChargesException manualChargesException=null;
			exception.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw manualChargesException;
		}
		return manualChargesList;
	}
	/**
	 * convert ManualChargesDetailsView to DTOs
	 * 
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws ManualChargesException
	 */
	public ManualChargesDetailsDTO convertManualChargesDetailsViewToDTOs(ManualChargesDetailsView view) throws ManualChargesException{
		ManualChargesDetailsDTO dto = null;
		try{
			if(view != null){
				dto = new ManualChargesDetailsDTO();
				dto.setManualChargeDetailId(view.getManualChargeDetailId());
				dto.setManualChargeId(view.getManualChargeId());
				dto.setPurchaseOrderVersionId(view.getPurchaseOrderVersionId());
				dto.setAmount(NumberUtil.parseDouble(view.getAmount()));
			}			
		}catch( Exception exception ){
			ManualChargesException manualChargesException=null;
			exception.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw manualChargesException;
		}
		return dto;
	}
	
}
